package com.aier.proxy.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolService;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.common.UserFinalPageSize;
import com.aier.user.entity.User;
import com.aier.user.service.IUserService;

@Scope("prototype")
@Component("proxySearchSchoolAction")
public class ProxySearchSchoolAction extends ProxyBaseAction {
    /**
     * 查询学校
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolService")
    private ISchoolService schoolService;
    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    
    @Resource(name = "userService")
    private IUserService userService;    

    private List<School> schoolsList;
    
    private Integer pid;
    private Integer sid;
    private String account;
    private String error_info;
    
    // 当前页
    private int page;

    // 每页显示记录数

    private int page_size;

    // 总页数

    private int page_count;

    // 总数

    private int count;
    
    //状态
    private int s;

    @Override
    public String execute() {

        return SUCCESS;
    }

    // 加载学校信息
    @SuppressWarnings("unchecked")
    public String selSc_load() {
        
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = schoolService.serachSchoolInfoBypid(pid, page, UserFinalPageSize.PROXY_SCHOOLS_PAGESIZE);
        schoolsList =  (List<School>) map.get(ICommonDAO.OBJ_LIST);
        if (null == schoolsList) {
            s = Constant.num_10000;
            return SUCCESS;
        }
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s=Constant.num_10001;
        return SUCCESS;
    }

    // 添加学校管理员
    public String addSchoolManage(){
        s = 0;
        Timestamp ctime = new Timestamp(new Date().getTime());
        User u = userService.searchUserByCondition(account);
        if(u==null){
            error_info = "该账号不存在";
            return SUCCESS;
        }else if(u.getIsbaby()==(short)1){
            error_info = "该账号非注册账号";
            return SUCCESS;
        }
        SchoolTeacher st = schoolTeacherService.searchSchoolTeacherByIds(u.getUid(), sid);
        if(st!=null){
            if(st.getIsadmin()==(short)1){
                error_info = "该账号已经是该学校管理员";
                return SUCCESS;
            }else{
                s = schoolTeacherService.updateSchoolTeacherState(st.getTid(), (short)1);
            }
        }else{
            s = schoolTeacherService.insertSchoolTeacher(new SchoolTeacher(null, u.getUid(), sid, u.getNickname(), null, ctime, (short)1));
        }
        
        return SUCCESS;
    }
    
    
    
    
    
    
    public List<School> getSchoolsList() {
        return schoolsList;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public int getCount() {
        return count;
    }

    public int getS() {
        return s;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public String getError_info() {
        return error_info;
    }




}